package com.chii.colorization.ui.fragment.MainActivity.images;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.bumptech.glide.Glide;
import com.chii.colorization.R;
import com.chii.colorization.ui.activity.PhotoActivity;
import com.chii.colorization.utils.GlideEngine;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class ImagesFragment extends Fragment {

    @BindView(R.id.text_images)
    TextView textImages;
    @BindView(R.id.show_photo)
    ImageView showPhoto;
    @BindView(R.id.floatingActionButton)
    FloatingActionButton floatingActionButton;
    private Unbinder unbinder;
    private ImagesViewModel slideshowViewModel;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        slideshowViewModel = ViewModelProviders.of(this).get(ImagesViewModel.class);
        View root = inflater.inflate(R.layout.fragment_images, container, false);
        unbinder = ButterKnife.bind(this, root);
        slideshowViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textImages.setText(s);
            }
        });
        return root;
    }

    @OnClick({R.id.floatingActionButton, R.id.show_photo})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.show_photo:
                break;
            case R.id.floatingActionButton:
                PictureSelector.create(this)
                        .openGallery(PictureMimeType.ofImage())
                        .isCamera(true)
                        // 单选
                        .selectionMode(PictureConfig.SINGLE)
                        .isSingleDirectReturn(true)
                        .loadImageEngine(GlideEngine.createGlideEngine())
                        .previewImage(true)
                        // 是否裁剪
                        .enableCrop(true)
                        // 裁剪比例 如16:9 3:2 3:4 1:1 可自定义
                        .withAspectRatio(1, 1)
                        // 是否显示uCrop工具栏，默认不显示
                        .hideBottomControls(false)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
            default:

        }
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case PictureConfig.CHOOSE_REQUEST:
                List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                String photoPath = selectList.get(0).getCutPath();
                File file = new File(photoPath);
                Glide.with(this).load(file).dontAnimate().into(showPhoto);

                break;
            default:
        }
    }

    @Override
    public void onDestroyView() {
        unbinder.unbind();
        super.onDestroyView();
    }

}